package com.coffee.admin.controller;

import com.coffee.admin.common.dto.LoginDTO;
import com.coffee.admin.common.vo.LoginVO;
import com.coffee.admin.service.IAppUserLoginService;
import com.coffee.framework.log.annotation.AutoLog;
import com.coffee.framework.common.result.R;
import com.coffee.framework.common.util.StpAppUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author Kevin
 */
@RestController
@RequestMapping("/auth/app")
@AllArgsConstructor
public class AppLoginController {

    private IAppUserLoginService appUserService;

    @PostMapping("/login")
    @AutoLog(module = "授权管理", title = "登录", isSaveRequestData = false)
    public R<LoginVO> login(@Validated @RequestBody LoginDTO req) {
        String token = appUserService.login(req);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        return R.success(loginVO);
    }

    @PostMapping("/logout")
    @AutoLog(module = "授权管理", title = "退出")
    public R<Void> logout() {
        try {
            StpAppUtil.logout();
        } catch (Exception ex) {
        }
        return R.success();
    }

}
